﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Data;

namespace Efficient.Excel
{
    internal enum SimpleType
    {
        Boolean,
        Byte,
        Char,
        DateTime,
        Decimal,
        Double,
        Float,
        Int16,
        Int32,
        Int64,
        String,
        Object,
        Unknown
    }

    internal class DataReaderValueGetter : IDataReaderValueGetter
    {
        private readonly SimpleType dataType = SimpleType.Unknown;

        internal SimpleType DataType
        {
            get { return dataType; }
        } 

        public DataReaderValueGetter(SimpleType dataType)
        {
            this.dataType = dataType;
        }

        #region IDataReaderValueGetter Members
        

        public object GetValue(IDataReader reader, string name)
        {
            return GetValue(reader, reader.GetOrdinal(name));
        }

        public object GetValue(IDataReader reader, int index)
        {
            switch (this.dataType)
            { 
                case SimpleType.Boolean:
                    return SafeConvert.ToBoolean(reader.GetValue(index).ToString());
                case SimpleType.Byte:
                    return SafeConvert.ToByte(reader.GetValue(index).ToString());
                case SimpleType.Char:
                    return reader.GetChar(index);
                case SimpleType.DateTime:
                    return SafeConvert.ToDateTime(reader.GetValue(index).ToString());
                case SimpleType.Decimal:
                    return SafeConvert.ToDecimal(reader.GetValue(index).ToString());
                case SimpleType.Double:
                    return SafeConvert.ToDouble(reader.GetValue(index).ToString());
                case SimpleType.Float:
                    return SafeConvert.ToSingle(reader.GetValue(index).ToString());
                case SimpleType.Int16:
                    return SafeConvert.ToInt16(reader.GetValue(index).ToString());
                case SimpleType.Int32:
                    return SafeConvert.ToInt32(reader.GetValue(index).ToString());
                case SimpleType.Int64:
                    return SafeConvert.ToInt64(reader.GetValue(index).ToString());
                case SimpleType.String:
                    return reader.GetValue(index).ToString();
                case SimpleType.Object:
                default: return reader.GetValue(index);
            }
        }

        #endregion


    }
}
